import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import parse.ADHandle;
import parse.BalanceHandle;
import parse.ChannelHandle;
import parse.CountyHandle;
import parse.CustomerHandle;
import parse.GroupHandle;
import parse.HallHandle;

import com.jspsmart.upload.SmartUpload;

public class UploadServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ServletConfig config;

	private com.jspsmart.upload.File file;

	private String filepath = "";

	private String doctype = "";

	/**
	 * Init the servlet
	 */
	final public void init(ServletConfig config) throws ServletException {

		this.config = config;
	}

	final public ServletConfig getServletConfig() {
		return config;
	}

	/**
	 * Handles GET requests
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	/**
	 * Handles POST requests
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String pagetype = request.getParameter("BBS");
		String filename="";
		if ("upload".equals(pagetype)) {
			doctype = request.getParameter("Type");
			createDir(doctype);
			SmartUpload mySmartUpload = new SmartUpload();
			try {
				mySmartUpload.initialize(config, request, response);
				mySmartUpload.upload();
				file = mySmartUpload.getFiles().getFile(0);
				if (file.getFileName() != null
						&& !file.getFileName().equals("")) {
					try {
						mySmartUpload.setMaxFileSize(2 * 1024 * 1024);
//						filename=new String((file.getFileName()).getBytes(),"GBK");
						filename=doctype;
						file.saveAs(filepath + filename,SmartUpload.SAVE_PHYSICAL);
					} catch (Exception ex) {
						ex.printStackTrace();
					}
					request.setAttribute("filepath", filepath
							+ filename);
					request.setAttribute("doctype", doctype);
					request.getRequestDispatcher("/index.jsp").forward(request,
							response);
				}
			} catch (Exception e) {
				try {
					request.setAttribute("msg", "Upload error");
					request.getRequestDispatcher("../index/error.jsp").forward(
							request, response);
				} catch (Exception ex) {
				}
			}
		}
		if ("import".equals(pagetype)) {
			String xmltype = request.getParameter("doctype");
			if ("1".equals(xmltype)) {
				BalanceHandle ex=new BalanceHandle();
				//System.out.println("starting....");
				String filepath = request.getParameter("filepath");
				ex.readExcel(filepath);
				ex.insertInfo();
			}
			if ("2".equals(xmltype)) {
				CountyHandle ex = new CountyHandle();
				//System.out.println("starting....");
				String filepath = request.getParameter("filepath");
				ArrayList al = ex.readExcel(filepath);
				ex.insertData(al);
			}
			if ("3".equals(xmltype)) {
				CustomerHandle ex = new CustomerHandle();
				//System.out.println("starting....");
				String filepath = request.getParameter("filepath");
				ArrayList al = ex.readExcel(filepath);
				ex.insertData(al);
			}
			if ("4".equals(xmltype)) {
				ADHandle ex = new ADHandle();
				//System.out.println("starting....");
				String filepath = request.getParameter("filepath");
				ArrayList al = ex.readExcel(filepath);
				ex.insertData(al);
			}
			if ("5".equals(xmltype)) {
				HallHandle ex = new HallHandle();
				//System.out.println("starting....");
				String filepath = request.getParameter("filepath");
				ex.readExcel(filepath);
				//ex.insertInfo();
			}
			if ("6".equals(xmltype)) {
				ChannelHandle ex=new ChannelHandle();
				//System.out.println("starting....");
				String filepath = request.getParameter("filepath");
				ex.readExcel(filepath);
				ex.insertInfo();
			}
			if ("7".equals(xmltype)) {
				GroupHandle ex = new GroupHandle();
				//System.out.println("starting....");
				String filepath = request.getParameter("filepath");
				ArrayList al = ex.readExcel(filepath);
				ex.insertData(al);
			}
		}
	}
	
	private boolean createDir(String dirName) {
		try {
			File file = new File("e:/doc/" + dirName);
			filepath = "e:/doc/" + dirName + "/";
			if (!file.exists())
				file.mkdirs();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * Destroy the servlet
	 */
	public void destroy() {
	}

}
